//
//  AllViewController.m
//  Article
//
//  Created by Haricot_cpc17 on 16/5/7.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AllViewController.h"
#import "AllFundDataTableViewCell.h"

#import "AllFundData2TableViewCell.h"
#import "CrashViewController.h"
#import "NoteViewController.h"
#import "FundDataInfoViewController.h"
@interface AllViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableDictionary *mutDic;

@end

@implementation AllViewController

-(void)viewWillAppear:(BOOL)animated{
    [self loadThisPageData];
    
  //  [self loadThisPageDataMoney];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mutDic = [[NSMutableDictionary alloc]init];
    
    self.view.backgroundColor = UIColorFromRGB(0xffffff);
    
    //[self downMJ];
    // Do any additional setup after loading the view.
}
//下拉刷新
-(void)downMJ{
    
    [self loadThisPageData];
 //   [self loadThisPageDataMoney];
    
    [_tableView.header endRefreshing];
}
//上拉加载
- (void)loadMoreData{
    [self loadThisPageData];
    [_tableView.footer endRefreshing];
}

-(void)loadThisPageData{
    
    _dataArray = [NSMutableArray arrayWithCapacity:0];;

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    NSMutableDictionary *mutDic = [[NSMutableDictionary alloc]init];
    
    [mutDic setObject:[user objectForKey:@"token"] forKey:@"user_token"];
    [mutDic setObject:@"" forKey:@"type"];
    [mutDic setObject:@"1" forKey:@"page"];
    
    [manager POST:@"http://wz.ijiaque.com/app/toup/brokerage.html" parameters:mutDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSString * str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"status"]];
        if ([str isEqualToString:@"1"]) {
            _dataArray = dic[@"data"];
        }
            [self loadThisPageDataMoney];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.description);
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        [self.view makeToast:@"网络连接失败，请检查网络连接" duration:0.5 position:CSToastPositionCenter];
    }];
}

-(void)loadThisPageDataMoney{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    NSMutableDictionary *mutDic = [[NSMutableDictionary alloc]init];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    [mutDic setObject:[user objectForKey:@"token"] forKey:@"user_token"];
        [manager POST:@"http://wz.ijiaque.com/app/toup/cashdes.html" parameters:mutDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _mutDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSString * str = [NSString stringWithFormat:@"%@",[_mutDic objectForKey:@"status"]];
        if ([str isEqualToString:@"1"]) {

        }
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            if (_tableView == nil) {
                [self createUI];
            }else{
                [_tableView reloadData];
            }
            
            if (_dataArray.count > 3) {
                [_tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.description);
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        [self.view makeToast:@"网络连接失败，请检查网络连接" duration:0.5 position:CSToastPositionCenter];
    }];
}

-(void)createUI{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 94) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(downMJ)];
    [_tableView registerNib:[UINib nibWithNibName:@"AllFundDataTableViewCell" bundle:nil] forCellReuseIdentifier:@"FundDataIDAllID"];
    [_tableView registerNib:[UINib nibWithNibName:@"AllFundData2TableViewCell" bundle:nil] forCellReuseIdentifier:@"allFund"];
    [self.view addSubview:_tableView];
    
    [self setExtraCellLineHidden:_tableView];
   
    _tableView.showsVerticalScrollIndicator = NO;
    
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        if (_dataArray.count <= 0) {
            return 0;
        }else{
            return _dataArray.count;
        }
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        AllFundDataTableViewCell *cellXib = [tableView dequeueReusableCellWithIdentifier:@"FundDataIDAllID" forIndexPath:indexPath];
        
        cellXib.FundDataLabel.text = [NSString stringWithFormat:@""];
        
        cellXib.FundDataLabel.text = [NSString stringWithFormat:@"￥%@",_mutDic[@"data"][@"账户资金"]];
        
        cellXib.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cellXib.userInteractionEnabled = NO;
        
        return cellXib;

    }
    
    AllFundData2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allFund" forIndexPath:indexPath];
    if (_dataArray.count<=0) {
        return cell;
    }
    cell.moneyLabel.text = _dataArray[indexPath.row][@"price"];
    cell.dataLabel.text = _dataArray[indexPath.row][@"create_time"];
    cell.infoLabel.text = _dataArray[indexPath.row][@"remark"];
   [cell.headerimager sd_setImageWithURL:[NSURL URLWithString:_dataArray[indexPath.row][@"userinfo"][@"avatar128"]]placeholderImage:[UIImage imageNamed:@"头像.png"]];

    cell.moneyLabel.font = [UIFont fontWithName:@"Heiti SC" size:14];
    cell.dataLabel.font = [UIFont fontWithName:@"Heiti SC" size:13];
    cell.infoLabel.font = [UIFont fontWithName:@"Heiti SC" size:11];
    
    
    
    return cell;
}
-(void)CarshClick:(UIButton *)btn{
    CrashViewController *crashVC = [[CrashViewController alloc]init];
    [self.navigationController pushViewController:crashVC animated:YES];
}
-(void)NoteClick:(UIButton *)btn{
    NoteViewController *noteVC = [[NoteViewController alloc]init];
    [self.navigationController pushViewController:noteVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 70;
    }
    return 90;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    FundDataInfoViewController *fundDataInfoVC = [[FundDataInfoViewController alloc]init];
    fundDataInfoVC.idStr = _dataArray[indexPath.row][@"id"];
    fundDataInfoVC.statusStr = _dataArray[indexPath.row][@"status"];
    
    [self.navigationController pushViewController:fundDataInfoVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
